package com.js.myapp.system.activity;

// Created by JS on 2021/1/26.

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.js.basicpage.activity.BaseLoadingDialogActivity;
import com.js.basicpage.activity.WatchVideoActivity;
import com.js.basicutils.FileUtils;
import com.js.basicutils.LogUtils;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.StatusBarUtil;
import com.js.basicutils.video.TakeVideoUtils;
import com.js.myapp.R;
import com.js.myapp.common.Const;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TakeVideoActivity extends BaseLoadingDialogActivity {

    @BindView(R.id.btn_take_video)
    Button btnTakeVideo;
    @BindView(R.id.btn_pick_video)
    Button btnPickVideo;
    @BindView(R.id.btn_net_video)
    Button btnNetVideo;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.iv_play)
    ImageView ivPlay;

    private TakeVideoActivity mContext;
    private static final int REQUEST_VIDEO_CODE = 6;
    private static final int SELECT_VIDEO_REQUEST_CODE = 7;
    private File videoFile;
    private boolean isNet = false; // 是否是网络视频
    private static final String netVideoUrl = "https://download.niucache.com/static/upload/20201229/d92c0c7a-27b2-486e-be4c-8767a4cd754b.mp4";

    public static void start(Context context) {
        Intent intent = new Intent(context, TakeVideoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_video);
        ButterKnife.bind(this);
        mContext = this;
        NavigationBarUtil.setSysNavigationColor(this, Color.parseColor("#abcdef"));
        StatusBarUtil.setStatusBarColor(this, R.color.color_statusbar_bg);
    }

    @OnClick({R.id.btn_take_video, R.id.btn_pick_video, R.id.btn_net_video, R.id.iv_play})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_take_video:
                isNet = false;
                TakeVideoUtils.takeVideo(mContext, 1, 10, 50, REQUEST_VIDEO_CODE);
                break;
            case R.id.btn_pick_video:
                isNet = false;
                TakeVideoUtils.selectVideo(mContext, SELECT_VIDEO_REQUEST_CODE);
                break;
            case R.id.btn_net_video:
                isNet = true;
                Glide.with(this)
                        .load(netVideoUrl)
                        .into(iv);
                ivPlay.setVisibility(View.VISIBLE);
                break;
            case R.id.iv_play:
                if (isNet) {
                    WatchVideoActivity.startActivity(this, netVideoUrl);
                } else {
                    WatchVideoActivity.startActivity(this, videoFile.getPath());
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_VIDEO_CODE:
                case SELECT_VIDEO_REQUEST_CODE:
                    if (data != null) setVideo(data);
                    break;
            }
        }
    }

    private void setVideo(Intent data) {
        assert data != null;
        Uri videoUri = data.getData();
        String realFilePath = FileUtils.getPath(this, videoUri);
        assert realFilePath != null;
        File file = new File(realFilePath);
        LogUtils.d(this, file.getPath());
        String newVideoPath = Const.APP_VIDEOS + System.currentTimeMillis() + ".mp4";
        FileUtils.copyFile(file.getPath(), newVideoPath);
        file.delete();
        videoFile = new File(newVideoPath);
        if (videoFile.exists()) {
            Glide.with(this)
                    .load(videoFile)
                    .into(iv);
            ivPlay.setVisibility(View.VISIBLE);
        }
    }
}
